﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using CNdotnet.Ezsocio.Core.DataEntity;

namespace CNdotnet.Ezsocio.DataEntities
{
    [Table(Name = "dbo.FriendGroup")]
    public class FriendGroup : Entity
    {
        #region Private Fields

        private int _Id;

        private int _userId;

        private string _groupName;

        private EntitySet<Friendship> _friendships;

        private EntityRef<User> _user;

        #endregion

        #region Constructor

        public FriendGroup()
        {
            this._friendships = new EntitySet<Friendship>(new Action<Friendship>(this.AttachFriendships), new Action<Friendship>(this.DetachFriendships));
            this._user = default(EntityRef<User>);
        }

        public FriendGroup(int id):this()
        {
                        
        }

        #endregion

        #region Public Properties

        [Column(Name = "ID", Storage = "_Id", AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
        public int ID
        {
            get
            {
                return this._Id;
            }
            set
            {
                if (this._Id != value)
                {
                    this.SendPropertyChanging("ID");
                    this._Id = value;
                    this.SendPropertyChanged("ID", value);
                }
            }
        }

        [Column(Name = "UserId", Storage = "_userId", DbType = "Int NOT NULL")]
        public int UserId
        {
            get
            {
                return this._userId;
            }
            set
            {
                if (this._userId != value)
                {
                    if (this._user.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }
                    this.SendPropertyChanging("UserId");
                    this._userId = value;
                    this.SendPropertyChanged("UserId", value);
                }
            }
        }

        [Column(Name = "GroupName", Storage = "_groupName", DbType = "VarChar(64) NOT NULL", CanBeNull = false)]
        public string GroupName
        {
            get
            {
                return this._groupName;
            }
            set
            {
                if ((this._groupName != value))
                {
                    this.SendPropertyChanging("GroupName");
                    this._groupName = value;
                    this.SendPropertyChanged("GroupName", value);
                }
            }
        }

        [Association(Name = "FriendGroup_Friendship", Storage = "_friendships", OtherKey = "GroupId")]
        public EntitySet<Friendship> Friendships
        {
            get
            {
                return this._friendships;
            }
            set
            {
                this._friendships.Assign(value);
            }
        }

        [Association(Name = "User_FriendGroup", Storage = "_user", ThisKey = "UserId", IsForeignKey = true)]
        public User User
        {
            get
            {
                return this._user.Entity;
            }
            set
            {
                User previousValue = this._user.Entity;
                if (((previousValue != value)
                            || (this._user.HasLoadedOrAssignedValue == false)))
                {
                    if ((previousValue != null))
                    {
                        this._user.Entity = null;
                        previousValue.FriendGroups.Remove(this);
                    }
                    this._user.Entity = value;
                    if ((value != null))
                    {
                        value.FriendGroups.Add(this);
                        this._userId = value.ID;
                    }
                    else
                    {
                        this._userId = default(int);
                    }
                }
            }
        }

        #endregion

        #region Attach and Detach Methods

        private void AttachFriendships(Friendship entity)
        {
            entity.FriendGroup = this;
        }

        private void DetachFriendships(Friendship entity)
        {
            entity.FriendGroup = null;
        }

        #endregion
    }
}
